using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ADiKt {

    /// <summary> 
    /// Diffines the basic entry type allowable in a menu 
    /// extended into normal menu entries and spiners
    /// </summary>
    abstract class Entry {
        public delegate void EventHandler(object sender);
        string text;
        float selectionFade;
        Vector2 position;

        public string Text {
            get { return text; }
            set { text = value; }
        }

        public Vector2 Position {
            get { return position; }
            set { position = value; }
        }


        /// <summary> fades the entry in/out of prominance </summary>
        public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime) {
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }

        /// <summary> draw the entry to the screen </summary>
        public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime) {
            Color color = isSelected ? Color.Blue : Color.DarkCyan;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;
            float pulsate = MathHelper.Clamp((float)Math.Log(time) , 0f, 0.2f);
            float scale = 1 + pulsate * selectionFade;

            // Modify the alpha to fade text out during transitions.
            color *= screen.TransitionAlpha;

            // Draw text, centered on the middle of each line.
            // Todo - center horizontally
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;
            Vector2 origin = new Vector2(font.MeasureString(text).X*(scale-1)/2, font.LineSpacing / 2); 
            spriteBatch.DrawString(font, text, position, color, 0, origin, scale, SpriteEffects.None, 0);
        }

        /// <summary> returns the height of the entry </summary>
        public virtual int GetHeight(MenuScreen screen) {
            return screen.ScreenManager.Font.LineSpacing;
        }

        /// <summary> returns the width of the entry </summary>
        public virtual int GetWidth(MenuScreen screen) {
            return (int)screen.ScreenManager.Font.MeasureString(Text).X;
        }
    }
}